/**
 * Copyright (c) 2010-2011 julewa.com.  
 * All rights reserved. 
 * 
 * @author Huang Benhua
 * @date 2011-2-20
 * 
**/
package com.julewa.db.ibatis.loading;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.julewa.db.BeanInfo;
import com.julewa.freemarker.FreemarkerManager;
import com.julewa.loading.SystemUtils;
import com.julewa.loading.TextsImpl;
import com.julewa.utils.ConfigUtils;
/**
 * 完全是另外一个系统,二者并不相干
 * 当使用者将本对象加入到spring的列中时
 * 这一层就自然起作用,简单情况下,通过spring.datasource=即可起效
 * 而transaction层的作用,通过struts的interceptor即可起效
 * 
 * @author huang benhua
 * 
 */
public class IbatisLoader {
	public static String PKG_PATH = IbatisLoader.class.getPackage().getName().replace('.', '/');
	private Properties conf = null;
	private SqlMapClient sqlMap = null;
	private Map<Class<?>, String> namespaces = null;
	private Map<String, BeanInfo> beans = null;
	@SuppressWarnings("unchecked")
	public IbatisLoader(Properties props, TextsImpl texts){
		conf = ConfigUtils.loadSystemConfig(PKG_PATH + "/julewa_ibatis.properties");
		ConfigUtils.combine(props, conf);
		//to load texts
		if(texts != null)for(Object k:conf.keySet()){
			String key = (String)k;
			if(!key.startsWith("label."))continue;
			texts.putLabel(key.substring(6), conf.getProperty(key));
		}
		//
		FreemarkerManager freemarker = new FreemarkerManager("/" + PKG_PATH);
		IbatisConfiguration ibatisconf = new IbatisConfiguration(conf, freemarker.load("stdsqlmap"));
		String xml = "_JULEWA_IBATIS_.xml";
		namespaces = ibatisconf.build(SystemUtils.getClassesPath() + xml, freemarker.load("ibatis"));
		this.loadRealConfig(xml);
		//注入式代码
		Map<String, IbatisBeanInfo> ibatisbeans = ibatisconf.getBeans();
		beans = new HashMap<String, BeanInfo>();
		for(IbatisBeanInfo ib : ibatisbeans.values()){
			if(ib == null)continue;
			beans.put(ib.getCode(), ib.getInfo());
		}
	}
	
	public Map<String, BeanInfo> getBeans(){
		return this.beans;
	}
	
	private void loadRealConfig(String xml){
		Reader reader;
		try {
			reader = Resources.getResourceAsReader (xml);
			sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public Map<Class<?>, String> getNamespaceMap(){
		return namespaces;
	}
	public SqlMapClient getSqlMapClient(){
		return sqlMap;
	}
	public static SqlMapClient loadIbatis(String conf){
		return new IbatisLoader(ConfigUtils.loadFullSystemConfig(conf), null).sqlMap;
	}
	public static String getCreationSql(String conf){
		Properties p = ConfigUtils.loadSystemConfig(PKG_PATH + "/julewa_ibatis.properties");
		ConfigUtils.combine(ConfigUtils.loadFullSystemConfig(conf), p);
		return new IbatisConfiguration(p, null).getCreationSql();
	}
}
